package pro5_decoder_encoder.ex3_objectSerialize;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @program: netty
 * @description:
 * @author: zy
 * @create: 2025-03-23 10:26
 */
public class SerializationServer {
    static final int PORT = 8089;

    private static final int MASTER_REACTOR_SIZE = 1;

    private static final int SLAVE_REACTOR_SIZE = Runtime.getRuntime().availableProcessors();

    private static final int BUSINESS_THREAD_SIZE = Runtime.getRuntime().availableProcessors() * 2;

    public static void main(String[] args) throws Exception {
        EventLoopGroup masterReactorGroup = new NioEventLoopGroup(MASTER_REACTOR_SIZE);
        //从reactor线程组(处理IO请求)
        EventLoopGroup slaveReactorGroup = new NioEventLoopGroup(SLAVE_REACTOR_SIZE);
        // 业务线程组(处理业务逻辑)
        EventLoopGroup businessGroup = new NioEventLoopGroup(BUSINESS_THREAD_SIZE);
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(masterReactorGroup, slaveReactorGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 100) // 服务端接受连接的队列长度，如果队列已满，客户端连接将被拒绝。
                    .childOption(ChannelOption.SO_KEEPALIVE, true) // 保持连接
                    .handler(new LoggingHandler(LogLevel.INFO)) // 记录日志
                    .childHandler(new SerializationServerInitializer(businessGroup));

            ChannelFuture f = b.bind(PORT).sync();

            f.channel().closeFuture().sync();
        } finally {
            masterReactorGroup.shutdownGracefully();
            slaveReactorGroup.shutdownGracefully();
        }
    }
}
